package com.yupi.yuaiagent.tool;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TerminalOperationTool {

    @Tool(description = "Executes a terminal command")
    public String executeTerminalCommand(@ToolParam(description = "Command to execute in the terminal") String command) {
        StringBuilder output = new StringBuilder();
        try {
            ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", command);
            Process process = builder.start();
            try(BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"))){
                String line = "";
                while ((line = stdout.readLine()) != null) {
                    output.append(line).append("\n");
                }
            }
            // 处理错误输出流
            try(BufferedReader stderrReader = new BufferedReader(new InputStreamReader(process.getErrorStream(),"GBK"))){
                String line = "";
                while ((line = stderrReader.readLine()) != null) {
                    output.append("Command execution failed with exit code: ").append(line).append("\n");
                }
            }
        } catch (Exception e) {
            output.append("Error executing command: ").append(e.getMessage());
        }
        return output.toString();
    }
}
